<?php
/**
 * 控制器类
 * TINPHP - A PHP Framework
 * @author   WU SHAIN <634976171@QQ.com>
 */
class Controller extends Smarty{
    //初始化参数
    function __construct(){
        parent::__construct();
        //设置定界符
        $this->left_delimiter  = LEFT_DELIMITER;
        $this->right_delimiter = RIGHT_DELIMITER;
        //设置模版目录
        $this->setTemplateDir(TEMPLATEDIR);
        //设置缓存以及时间
        $this->cache_lifetime  = CACHE_LIFETIME;
        $this->caching=CACHING;
    }

    //重写父类display,向模版输出根目录{{$root}}
    function display($template = NULL, $cache_id = NULL, $compile_id = NULL, $parent = NULL){
        /*public 目录*/
        $this->assign("public", URL_ROOT.'/public');
        /*根目录*/
        $this->assign("root", URL_ROOT);
        /*站点根网址*/
        $this->assign("site", SITE_URL);
        parent::display($template, $cache_id, $compile_id,$parent);
    }

    //重定向
    function redirect($controller,$method){
        $url=SITE_URL.'/'.$controller.'/'.$method;
        echo '<script>';
        echo 'location="'.$url.'"';
        echo '</script>';
    }

    //跳转提示页面
    function jump($msg,$controller,$method){
        $this->assign('msg',$msg);
        $this->assign('c',$controller);
        $this->assign('m',$method);
        $this->display('tpl/jump.php');
    }

    //Ajax返回值
    function ajaxReturn($data,$type='') {
        if(func_num_args()>2) {
            $args           =   func_get_args();
            array_shift($args);
            $info           =   array();
            $info['data']   =   $data;
            $info['info']   =   array_shift($args);
            $info['status'] =   array_shift($args);
            $data           =   $info;
            $type           =   $args?array_shift($args):'';
        }
        if(empty($type)) $type  =   'JSON';
        switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($data);
            default     :
                // 用于扩展其他返回格式数据
        }
    }
}
